
#ifndef __TAL_NV_FLASH_INNER_H__
#define __TAL_NV_FLASH_INNER_H__


#ifdef __cplusplus
    extern "C" {
#endif

#include "tuya_cloud_types.h"

typedef enum {
    TAL_NV_SUCC = 0,
    TAL_NV_INVALID_MODULS,
    TAL_NV_INVALID_ID ,
    TAL_NV_ITEM_NOT_FOUND,
    TAL_NV_NOT_ENOUGH_SAPCE,
    TAL_NV_ITEM_LEN_NOT_MATCH,
    TAL_NV_CHECK_SUM_ERROR,
	TAL_NV_ERROR_OTHERS = 0xFF
} TAL_NV_STATUS_T;



//If OTA enabled, the maximum space used for nv module t is 56K, thus the item num cannot over 14
/********************************************************************
 * mg21-1M: store zigbee information in flash.
 * 		Module ID				|      flash partition size  	    |
 * -----------------------------|-----------------------------------|
 * TUYA_NV_MODULE_USER    		|	  0x000E8000 - 0x000EC000-1		| // TAL_MODULE0_NV_ITEM_T
 * TUYA_NV_MODULE_ATTR    		|	  0x000EC000 - 0x000F0000-1		| // TAL_MODULE0_NV_ITEM_T
 * TUYA_NV_MODULE_TABLE    		|	  0x000F0000 - 0x000F4000-1		| // TAL_MODULE1_NV_ITEM_T
 * TUYA_NV_MAX_MODULS
 */

/********************************************************************
 * mg21-768K: store zigbee information in flash.
 * 		Module ID				|      flash partition size  	    |
 * -----------------------------|-----------------------------------|
 * TUYA_NV_MODULE_USER    		|	  0x000A8000 - 0x000AC000-1		| // TAL_MODULE0_NV_ITEM_T
 * TUYA_NV_MODULE_ATTR    		|	  0x000AC000 - 0x000B0000-1		| // TAL_MODULE0_NV_ITEM_T
 * TUYA_NV_MODULE_TABLE    		|	  0x000B0000 - 0x000B4000-1		| // TAL_MODULE1_NV_ITEM_T
 * TUYA_NV_MAX_MODULS
 */
 
 /********************************************************************
 * PHY622X: store zigbee information in flash.
 * 		Module ID				|      flash partition size  	    |
 * -----------------------------|-----------------------------------|
 * TUYA_NV_MODULE_USER    		|	  0x000E2000 - 0x000E4000-1		| // TAL_MODULE0_NV_ITEM_T //2 page
 * TUYA_NV_MODULE_ATTR    		|	  0x000E4000 - 0x00060000-1		| // TAL_MODULE0_NV_ITEM_T //2 page
 * TUYA_NV_MODULE_TABLE    		|	  0x000e6000 - 0x000e8000-1		| // TAL_MODULE1_NV_ITEM_T //2 page
 * TUYA_NV_MAX_MODULS
 */

/********************************************************************
 * Telink8258: Store zigbee information in flash.
 * 		Module ID				|      flash partition size  	    |
 * -----------------------------|-----------------------------------|
 * TUYA_NV_MODULE_USER    		|	  0x000E8000 - 0x000EA000-1		| // TAL_MODULE0_NV_ITEM_T
 * TUYA_NV_MODULE_ATTR    		|	  0x000EA000 - 0x000EC000-1		| // TAL_MODULE0_NV_ITEM_T
 * TUYA_NV_MODULE_TABLE    		|	  0x000EC000 - 0x000EE000-1		| // TAL_MODULE1_NV_ITEM_T
 * TUYA_NV_MAX_MODULS
 */

typedef enum {
    TUYA_NV_MODULE_USER     = 0,
    TUYA_NV_MODULE_ATTR     = 1,
	
#ifdef EFR32MG2X_EVB	
	TUYA_NV_MODULE_TABLE    = 1,//silicon:attr and table use a same module id
	TUYA_NV_MAX_MODULS      = 2,
#else
    TUYA_NV_MODULE_TABLE    = 2,
    TUYA_NV_MAX_MODULS
#endif
} TAL_NV_MODULE_T;

#ifndef TAL_NV_ATTR_ITEM_SUMS
#define TAL_NV_ATTR_ITEM_SUMS	100	// tuya nv attribute number
#endif


#ifdef MCU_CORE_8258
#if END_DEVICE
#ifndef TAL_NV_SCENE_TB_MAX
#define	TAL_NV_SCENE_TB_MAX		16
#endif
#else
#ifndef TAL_NV_SCENE_TB_MAX
#define	TAL_NV_SCENE_TB_MAX		32
#endif
#endif
#else
#ifndef TAL_NV_SCENE_TB_MAX
#ifdef EFR32MG2X_EVB
#define TAL_NV_SCENE_TB_MAX     50 // MUST EQUAL TO : EMBER_AF_PLUGIN_SCENES_TABLE_SIZE
#else
#define TAL_NV_SCENE_TB_MAX     32	// scene table max
#endif
#endif
#endif


typedef enum {
	TUYA_NV_INVALID0_ID		= 0,	/* Item id 0 should not be used. */
	// nv attribute information id
	TUYA_NV_ATTR_INFO_ID	= 1,
	// nv attribute base id
	TUYA_NV_ATTR_BASE_ID	= 2,
	// nv attr id...
	TUYA_NV_ATTR_MAX_ID		= (TUYA_NV_ATTR_BASE_ID+TAL_NV_ATTR_ITEM_SUMS),
	
	// user flash nv id
	TUYA_NV_USER_FLASH_ID,

	// network info nv id
	TUYA_NV_NWK_INFO_ID,
	
	// error code nv id
	TUYA_NV_ERROR_CODE_ID,

	// ecc sign nv id
	TUYA_NV_ECC_SIGN_CODE_ID,

	// mf test flag
	TUYA_NV_MF_TEST_FLAG_ID,
	
	// gateway information
	TUYA_NV_GW_INFO_ID,

#ifdef EFR32MG2X_EVB
	TUYA_NV_SCENE_TB_ITEM_BASE_ID,

	// sdk information nv id
	TUYA_NV_SDK_INFO_ID =(TUYA_NV_SCENE_TB_ITEM_BASE_ID+TAL_NV_SCENE_TB_MAX),

#endif
	TUYA_NV_ID_MAX0			= 0xFF,	/* Item id 0xFF should not be used. */
} TAL_MODULE0_NV_ITEM_T;

#ifndef EFR32MG2X_EVB
typedef enum {
	TUYA_NV_INVALID1_ID				= 0,	/* Item id 0 should not be used. */
	// scene table item base nv id
	TUYA_NV_SCENE_TB_ITEM_BASE_ID	= 1,

	// sdk information nv id
	TUYA_NV_SDK_INFO_ID = TUYA_NV_SCENE_TB_ITEM_BASE_ID + TAL_NV_SCENE_TB_MAX,

    TUYA_NV_RECOVERY_TIMES_ID       = 0xA1,

#ifdef PHY622X_EVB
    TUYA_NV_RECOVERY_INFO_ID        = 0xF1,
    TUYA_OUT_OF_BAND_JOIN_FLAG_ID   = 0xF2,
#endif

	TUYA_NV_ID_MAX1					= 0xFF,	/* Item id 0xFF should not be used. */
} TAL_MODULE1_NV_ITEM_T;
#endif

TAL_NV_STATUS_T tal_nv_reset_all(VOID_T);
TAL_NV_STATUS_T tal_nv_reset_module(UINT8_T modules);
TAL_NV_STATUS_T tal_nv_flash_delete(UINT8_T id, UINT8_T item_id);
TAL_NV_STATUS_T tal_nv_flash_read(UINT8_T id, UINT8_T item_id, UINT16_T len, UINT8_T *buf);
TAL_NV_STATUS_T tal_nv_flash_write(UINT8_T id, UINT8_T item_id, UINT16_T len, UINT8_T *buf);

#ifdef __cplusplus
}
#endif

#endif


